<?php require RCMS_Object_QuickConfig_QuickConfig::PATH_HEADER; ?>
<?php $this->headLink()->prependStylesheet($this->websiteUrl . 'system/css/seotoaster.css'); ?>
<?php $this->headLink()->appendStylesheet($this->websiteUrl .'system/css/dataTables.css'); ?>
<?php $this->headLink()->appendStylesheet($this->websiteUrl .'system/css/jquery-ui-theme/jquery-ui-1.8.5.custom.css'); ?>
<?php echo $this->headLink(); ?>
<?php $this->headScript()->prependFile(RCMS_Object_QuickConfig_QuickConfig::G_JQ_HOSTED_URL); ?>
<?php $this->headScript()->captureStart() ?>
	!window.jQuery && document.write(unescape('%3Cscript src="<?php echo $this->websiteUrl;?>system/js/jquery/jquery.js"%3E%3C/script%3E'))
<?php $this->headScript()->captureEnd() ?>
<?php $this->headScript()->appendFile(RCMS_Object_QuickConfig_QuickConfig::G_JQUI_HOSTED_URL); ?>
<?php $this->headScript()->captureStart() ?>
	!$.ui && document.write(unescape('%3Cscript src="<?php echo $this->websiteUrl;?>system/js/jquery/jquery-ui.js"%3E%3C/script%3E'))
<?php $this->headScript()->captureEnd() ?>
<?php $this->headScript()->appendFile($this->websiteUrl .'system/js/quote/quote.js'); ?>
<?php $this->headScript()->appendFile($this->websiteUrl .'system/js/jquery/plugins/DataTables/jquery.dataTables.min.js'); ?>
<?php echo $this->headScript(); ?>

       
<div id="seotoaster" class="shoppingquotes">
	<div class="success" style="display: none;" id="ajaxmsg"></div>
	<input type="hidden" id="wsurl-hidden" value="<?php echo $this->websiteUrl; ?>"/>
    <div class="admineditwhat" style="margin-bottom: 0px;">{%Manage quotes%}&nbsp;&nbsp;<a id="add-new-quote" href="javascript:;">{%Add new%}</a></div>
    <div class="close_window" onclick="top.tb_remove();"></div>
	<div id="dialog_invoice" class="fformated-main" style="float: none !important;">
		<input type="hidden" name="quote_id" id="quote_id" />
		<div class="field">
			<label for="invoice_id">{%Invoice%} {%id%}:</label>
			<input type="text" name="invoice_id" id="invoice_id" />
		</div>
		<div class="field">
			<label for="payment_method">{%Payment method%}:</label>
			<input type="text" name="payment_method" id="payment_method" />
		</div>
		<div class="field">
			<label for="shipping_tax_rate">{%Shipping tax rate%}, %:</label>
			<input type="text" name="shipping_tax_rate" id="shipping_tax_rate" />
		</div>
	</div>
    <div id="quoteholder">
		<table border="0" id="quotetable" cellpadding="0" cellspacing="2">
            <thead>
                <tr>
                    <th>#</th>
                    <th>{%Name%}</th>
                    <th>{%Date%}</th>
                    <th>{%Status%}</th>
                    <th>{%Preview%}</th>
                    <th style="white-space: nowrap">{%Last edited by%}</th>
                    <th>&nbsp;</th>
                    <th>&nbsp;</th>
                </tr>
            </thead>
            <?php //if (!empty($this->quotesData)): ?>
            <tbody>
                
            </tbody>
            <?php //endif; ?>
        </table>
    </div>
</div>
<script type="text/javascript">
    $(document).ready(function(){
		var previewTmpl = '<a target="_blank" href="${preview">{%Preview%}</a>';
		var statusSelect = ' <?php echo str_replace(PHP_EOL, " ", $this->formSelect(null,null, array('id'=>'qstat-${id}', 'class'=>array('qstatus','highlight-${status}')), $this->quotesAvstatuses)); ?> ';
		var quoteTable = $('table#quotetable').dataTable({
			"iDisplayLength": 20,
			"bJQueryUI": false,
			"bAutoWidth": false,
            "bProcessing": true,
            "bLengthChange": false,
			"bServerSide": true,
			"sAjaxSource": "<?php echo $this->websiteUrl;?>sys/backend_quote/quotelist/",
            "sPaginationType": "full_numbers",
			"fnServerData": function ( sSource, aoData, fnCallback ) {
				$.ajax( {
					"dataType": 'json',
					"type": "POST",
					"url": sSource,
					"data": aoData,
					"success": function(responce){
						var data = [];
						for ( var i in responce.aaData){
							//postprocessing data
							var qLink = '<a href="<?php echo $this->websiteUrl;?>sys/backend_quote/edit/qid/${id}" target="_blank">${name}</a>';
							var invButton = '<a class="invoice" title="{%Invoice%}" href="javascript:;"><img style="border:none" src="<?php echo $this->websiteUrl;?>system/images/invoice.png" alt="{%Invoice%}"/></a>';
							var delButton = '<a href="javascript:;" title="{%Delete%}" class="delquote" id="qdel-${id}"><img style="border:none" src="<?php echo $this->websiteUrl;?>system/images/ico_delete.gif" alt="{%Delete%}" /></a>'.replace('${id}', responce.aaData[i].id);
							data.push([
								responce.aaData[i].id
								, qLink.replace('${name}',responce.aaData[i].name).replace('${id}',responce.aaData[i].id)
								, responce.aaData[i].date
								, statusSelect.replace('${id}', responce.aaData[i].id).replace('${status}', responce.aaData[i].status).replace('value="'+responce.aaData[i].status+'"', 'value="'+responce.aaData[i].status+'" selected="true')
								, responce.aaData[i].preview !== false ? '<a target="_blank" href="${preview}">{%Preview%}</a>'.replace('${preview}', responce.aaData[i].preview) : '{%No preview%}'
								, responce.aaData[i].last_edited_by
								, responce.aaData[i].invoice ? invButton : ''
								, delButton
							]);
						}
						responce.aaData = data;
						fnCallback(responce);
					}
				} );
			},
			"aoColumnDefs": [
				{ "bSearchable": false, "bSortable": false, "sClass": "iconz", "aTargets": [-1, -2] },
				{ "bSortable": false, "aTargets": [3] },
				{ "sClass": "quoteid", "aTargets": [0] },
				{ "sClass": "lefted", "sType": "date", "aTargets": [1] },
				{ "sType": "date", "aTargets": [1] }
			],
			"aaSorting": [[ 2, "desc" ]]
        });

		$('#dialog_invoice').dialog({
			modal: true,
			resizable: false,
			draggable: false,
			height: 180,
			width: 350,
			title: '{%Create invoice%}',
			autoOpen: false,
			buttons: [
				{
					text: "{%OK%}",
					click: function(){
						var quoteId			= $('#quote_id').val();
						var invoiceId		= $('#invoice_id').val();
						var paymentMethod	= $('#payment_method').val();
						var shippingTaxRate	= $('#shipping_tax_rate').val();
						
						paymentMethod = paymentMethod.replace(/^\s+/, '');
						paymentMethod = paymentMethod.replace(/\s+$/, '');

						if (/^\d+$/.test(invoiceId) === false){
							$('#invoice_id').focus().prev('label').animate({color: 'red'}, 800).animate({color: '#7c7c7c'}, 600);
							return;
						}
						if (paymentMethod == ''){
							$('#payment_method').focus().prev('label').animate({color: 'red'}, 800).animate({color: '#7c7c7c'}, 600);
							return;
						}
						if (shippingTaxRate.length && isNaN(shippingTaxRate)){
							$('#shipping_tax_rate').focus().prev('label').animate({color: 'red'}, 800).animate({color: '#7c7c7c'}, 600);
							return;
						}
						$(this).dialog("close");
						top.location.href='<?php echo $this->websiteUrl;?>sys/backend_quote/pdf/type/quote/id/'+quoteId+'/title/invoice/customId/'+invoiceId+'/payment/'+paymentMethod+'/shippingtaxrate/'+parseFloat(shippingTaxRate)+'/';
					}
				},
				{
					text: "{%Cancel%}",
					click: function(){$(this).dialog("close");}
				}
			],
			beforeClose: function(event, ui) {
				$(this).find('#quote_id').val('');
			}
		});

		$('#add-new-quote').click(function(){
			var newWindow = window.open('<?php echo $this->websiteUrl; ?>sys/backend_quote/addnew');
			newWindow.focus();
		})
    });
</script>